﻿using BLL;
using DAL;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace WeChat.Ajax
{
    /// <summary>
    /// MerchantManagement 的摘要说明
    /// </summary>
    public class MerchantManagement : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //获取openid
            //string openid = context.Session["openid"] == null ? "" : context.Session["openid"].ToString();
            string openid = context.Session["UserName"] == null ? "" : context.Session["UserName"].ToString();
            //处理方法类型
            string merType = context.Request["merType"]==null ? "" : context.Request["merType"];
           
            //响应数据集
            string response = string.Empty;
            switch (merType) 
            {
                case "merhome":
                    JObject jo = new JObject(); 
                    //获取选中日期
                    string[] timearry = context.Request["date1"]==null ? "".Split('至') : context.Request["date1"].Split('至');
                    if (timearry.Length == 2 && !string.IsNullOrEmpty(openid))
                    {
                        string starttime = timearry[0] + " " + "00:00:00";
                        string endtime = timearry[1] + " " + "23:59:59";
                        var dataset = MerchantManagementBLL.MerHomeData(openid, starttime, endtime);
                        jo.Add("merdata1", JsonConvert.SerializeObject(dataset.Tables[0]));
                        jo.Add("merdata2", JsonConvert.SerializeObject(dataset.Tables[1]));
                        jo.Add("merdata3", JsonConvert.SerializeObject(dataset.Tables[2]));
                        jo.Add("merdata4", JsonConvert.SerializeObject(dataset.Tables[3]));
                        jo.Add("merdata5", JsonConvert.SerializeObject(dataset.Tables[4]));
                        response = jo.ToString();
                    }
                    else 
                    {
                        response = "-1";
                    }
                    break;
                case "infocanteen":
                    if (!string.IsNullOrEmpty(openid))
                    {
                        var infcandata= MerchantManagementBLL.InfoCanteen(openid);
                        var infcanstr = "请选择,";
                        foreach (DataRow item in infcandata.Rows)
                        {
                            infcanstr += item["wz"]+",";
                        }
                        infcanstr = infcanstr.Substring(0, infcanstr.Length - 1);
                        response = infcanstr;
                    }
                    else 
                    {
                        response = "-1";
                    }
                    break;


                case "infodangkou":
                    if (!string.IsNullOrEmpty(openid))
                    {
                        var infcandata = MerchantManagementBLL.InfoDangkou(openid);
                        var infcanstr = "请选择,";
                        foreach (DataRow item in infcandata.Rows)
                        {
                            infcanstr += item["zm"] + ",";
                        }
                        infcanstr = infcanstr.Substring(0, infcanstr.Length - 1);
                        response = infcanstr;
                    }
                    else
                    {
                        response = "-1";
                    }
                    break;
                case "initcantdata":
                   jo = new JObject();
                    //获取选中日期
                   timearry = context.Request["date1"] == null ? "".Split('至') : context.Request["date1"].Split('至');
                    if (timearry.Length == 2 && !string.IsNullOrEmpty(openid)) 
                    {
                        string cantname = context.Request["cantname"];
                        string starttime = timearry[0] + " " + "00:00:00";
                        string endtime = timearry[1] + " " + "23:59:59";
                        //获取食堂汇总
                        var dataset = MerchantManagementBLL.InfoCandata(openid, cantname, starttime, endtime);
                        jo.Add("cantsum", JsonConvert.SerializeObject(dataset.Tables[0]));
                        jo.Add("cantdata", JsonConvert.SerializeObject(dataset.Tables[1]));
                        response = jo.ToString();
                    }
                    else
                    {
                        response = "-1";
                    }
                    break;

                case "initdangkoudata":
                    jo = new JObject();
                    //获取选中日期
                    timearry = context.Request["date1"] == null ? "".Split('至') : context.Request["date1"].Split('至');
                    if (timearry.Length == 2 && !string.IsNullOrEmpty(openid))
                    {
                        string cantname = context.Request["cantname"];
                        string starttime = timearry[0] + " " + "00:00:00";
                        string endtime = timearry[1] + " " + "23:59:59";
                        //获取档口汇总
                        var dataset = MerchantManagementBLL.InfoDangkouData(openid, cantname, starttime, endtime);
                        jo.Add("cantsum", JsonConvert.SerializeObject(dataset.Tables[0]));
                        jo.Add("cantdata", JsonConvert.SerializeObject(dataset.Tables[1]));
                        response = jo.ToString();
                    }
                    else
                    {
                        response = "-1";
                    }
                    break;



            }

            context.Response.Write(response);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}